/**
 * @returns {String} date and time
 */
function get_date() {
    let date = new Date();
    let str = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日"+ " "+date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒";
    return str;
}

/* register the component root_comment */
Vue.component('root_comment', {
    template: "#root_comment",
    data:function(){
        return {
            num_nice: 0,
            child_comment_switch: false,
            // arr_cld_cmts: [],
            arr_cld_cmts: ["哈哈哈啊哈"],
            cld_cmt: ""
        }
    },
    props: ["receive"],
    methods:{
        nice: function(){
            this.num_nice++;
        },
        child_cmt: function(){
            this.child_comment_switch = true;
        },
        child_sub: function(){
            this.arr_cld_cmts.push(this.cld_cmt);
            this.cld_cmt = "";
            this.child_comment_switch = false;
        },
        child_del_comment: function(){
            let dom = this.$el;
            let idx = $(dom).index();
            this.$emit("send_to_parent", idx);
        },
        child_go_top: function(){
            let dom = this.$el;
            let idx = $(dom).index();
            this.$emit("send_goup_to_parent", idx);
        }
    }
});

new Vue({
    el: '#app',
    data:{
        arr_comments: [
            {name:"Tom", date:"2019年6月15日 15时15分34秒", comment: "第1条评论"},
            {name:"Jack", date:"2019年6月15日 15时16分39秒", comment: "第2条评论"},
            {name:"Peter", date:"2019年6月15日 15时17分39秒", comment: "第3条评论"}
        ],
        root_comment: ""
    },
    computed:{},
    watch:{},
    methods:{
        root_submit(){
            let date = get_date();
            let comment = this.root_comment;
            let cmt_obj={
                name: "XXX",
                date: date,
                comment: comment
            };
            this.arr_comments.push( cmt_obj );
            this.root_comment = "";
        },
        parent_del_comment(val){
            console.log('parent_del_comment', val);
            this.arr_comments.splice(val, 1);
        },
        parent_top(val){
            console.log('parent_top', val);
            let move = this.arr_comments[val];
            this.arr_comments.splice(val, 1);
            this.arr_comments.unshift(move);
        }
    }
})